package com.woupla.city;

import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import com.woupla.model.City;
import com.woupla.model.Region;
import org.jboss.solder.logging.Logger;


@Stateless
public class CityEJB {
	@Inject
	private Logger log;

	@Inject
	private EntityManager em;
	
	public List<Region> findRegionLike(String valueSearch){
		TypedQuery<Region> query =  em.createNamedQuery("findAllRegLike",Region.class);
		//TypedQuery<Region> query =  em.createQuery("Select r from Region r where r.label like 'H%'",Region.class);
		query.setParameter("reg", valueSearch+"%");
		query.setMaxResults(20);
		List<Region> regions = query.getResultList();
		return regions;
		
	}
	public List<City> findCityLike(String valueSearch){
		TypedQuery<City> query =  em.createNamedQuery("findAllCityLike",City.class);
		//TypedQuery<Region> query =  em.createQuery("Select r from Region r where r.label like 'H%'",Region.class);
		query.setParameter("reg", valueSearch+"%");
		query.setMaxResults(20);
		List<City> cities = query.getResultList();
		return cities;
		
	}
	public List<City> findCityLikeCP(String valueSearch){
		TypedQuery<City> query =  em.createNamedQuery("findAllCityLikeCP",City.class);
		//TypedQuery<Region> query =  em.createQuery("Select r from Region r where r.label like 'H%'",Region.class);
		query.setParameter("cp", valueSearch+"%");
		query.setMaxResults(20);
		List<City> cities = query.getResultList();
		return cities;
		
	}
	public Region findRegionLabel(String label){
		TypedQuery<Region> query =  em.createNamedQuery("findRegLabel",Region.class);
		query.setParameter("reg", label);
		return query.getSingleResult();
	}
	public City findCityLabel(String label){
		TypedQuery<City> query =  em.createNamedQuery("findCityLabel",City.class);
		query.setParameter("city", label);
		return query.getSingleResult();
	}
	public List<City> findCityByCP(String label){
		TypedQuery<City> query =  em.createNamedQuery("findAllCityCP",City.class);
		query.setParameter("cp", label);
		return query.getResultList();
	}
	public City findCityByCPAndLabel(String label,String cp){
		TypedQuery<City> query =  em.createNamedQuery("findAllCityByCPAndLabel",City.class);
		query.setParameter("cp", cp);
		query.setParameter("label", label);
		return query.getSingleResult();
	}
	public List<City> findCityLikeCpOrLabel(String value){
		TypedQuery<City> query =  em.createNamedQuery("city.findAllCityLikeCpOrLabel",City.class);
		System.out.println(value);
		//System.out.println(cp);
		query.setParameter("value", value + "%");
		//query.setParameter("city", "%");		
		query.setMaxResults(20);
		List<City> cities = query.getResultList();
		return cities;
	}
}
